package com.int_tech.facility.Service;

import com.int_tech.car.dao.CarDao;
import com.int_tech.facility.dao.FacilityDao;
import com.int_tech.facility.entity.Facility;
import com.int_tech.utils.CarEnum;
import com.int_tech.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by Administrator on 2017/9/25.
 */
@Service
public class FacilityService {

    @Autowired
    private CarDao carDao;

    @Autowired
    private FacilityDao facilityDao;

    //分页显示条数
    @Value("${size}")
    private int size;

    /**
    * TODO 添加设备信息
    * @param facility
    * @return Result
    * @author dmy
    */
    public Result SaveFacility(Facility facility){
        try {
                //根据车牌号验证车辆信息
        		if (carDao.findCarByCarNumberAndIsDel(facility.getCarNumber(), CarEnum.ON.getCode())==null){
        		    return Result.build(201,"车辆信息不存在");
                }
                facility.setCreatedTime(new Date());
        		facility.setUpdatedTime(new Date());
                facilityDao.save(facility);
        		return Result.ok();
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
        	return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据设备id逻辑删除设备信息
    * @param id
    * @return Result
    * @author dmy
    */
    @Transactional
    public Result deleteFacility(Integer id){
        try {
            Facility facility = facilityDao.findOne(id);
            facility.setIsDel(CarEnum.OFF.getCode());
            facilityDao.save(facility);
            return Result.ok();
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 修改设备信息
    * @param facility
    * @return Result
    * @author dmy
    */
    public Result updateFacility(Facility facility){
        try {
            //根据车牌号验证车辆信息
            if (carDao.findCarByCarNumberAndIsDel(facility.getCarNumber(), CarEnum.ON.getCode())==null){
                return Result.build(201,"车辆信息不存在");
            }
            facility.setUpdatedTime(new Date());
            facilityDao.save(facility);
            return Result.ok();
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 查询所有设备信息
    * @return Result（List<Facility>）
    * @author dmy
    */
    public Result findAllFacility(){
        try {
            return  Result.ok(facilityDao.findAllByIsDel(CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 分页查询设备信息
    * @param currentPage 当前页
    * @return Result（List<Facility>）
    * @author dmy
    */
    public Result fingByPage(int currentPage){
        try {
            PageRequest pageRequest = new PageRequest(currentPage-1,size);
            return Result.ok(facilityDao.findFacilityByIsDel(pageRequest,CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 根据设备序列号查询设备信息
    * @param facilityNumber
    * @return Result（Facility）
    * @author dmy
    */
    public Result findFacilityByFacilityNumber(String facilityNumber){
        try {
        	return  Result.ok(facilityDao.findFacilityByFacilityNumberAndIsDel(facilityNumber,CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据车牌号查询设备信息
    * @param carNumber
    * @return Result（Facility）
    * @author dmy
    */
    public Result findFacilityByCarNumber(String carNumber){
        try {
        		return Result.ok(facilityDao.findFacilityByCarNumberAndIsDel(carNumber,CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
     * TODO 分页
     * @param pageable
     * @return
     * @author dmy
     */
    public Page<Facility> getFacilityByPageable(Pageable pageable){
        Page<Facility> facilityPage = facilityDao.findByIsDel(pageable,CarEnum.ON.getCode());
        return facilityPage;
    }
}
